<?php
// +----------------------------------------------------------------------
// | Bwsaas
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2020 http://www.buwangyun.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Gitee ( https://gitee.com/buwangyun/bwsaas )
// +----------------------------------------------------------------------
// | Author: buwangyun <hnlg666@163.com>
// +----------------------------------------------------------------------
// | Date: 2020-9-28 10:55:00
// +----------------------------------------------------------------------

namespace app\manage\controller\member;

use buwang\base\MemberBaseController;
use app\manage\model\Config as ConfigModel;

class Config extends MemberBaseController
{
    // 功能移到base当中，方便继承.
    /*protected $middleware = [
        'login' => ['except' => []]
    ];*/
    protected $saveDataUid = 0;


    public function showMemberConfig($tab_name = null, $type = null, $config_ids = null, $app = null, $plugin = null)
    {
        //var_dump($tab_name, $type, $config_ids,$app,$plugin);die;
        $tab_name = $tab_name ?: ($this->request->param('tab_name') ?: null);//配置分类
        $type = $type ?: ($this->request->param('type') ?: null);//配置类型
        $config_ids = $config_ids ?: ($this->request->param('config_ids') ?: null);//配置id
        $app = $app ?: ($this->request->param('app') ?: null);//app标识
        $plugin = $plugin ?: ($this->request->param('plugin') ?: null);//插件标识

        // 20230621 判断是不是租户后台请求
        $member_id = 0;
        if ($this->scopes == "member") {
            $member_id = $this->uid;//获取中间件自动挂取的租户用户ID
        }
        //获取租户应用目录
        if (!$app) {
            $app_temp = app('http')->getName();
            $app = !in_array($app_temp, ['manage', 'install', 'api', 'home']) ? $app_temp : $app;
        }
        $data = ConfigModel::getShowData($tab_name, $type, $member_id, 'member', $config_ids, $app);

        //如果传了插件标识，则优先取插件下的配置模板
        if ($plugin) {
            $configFile = ADDON_PATH . $plugin . DS . 'view' . DS . 'config' . DS . 'config.html';
            $viewFile = is_file($configFile) ? $configFile : '';
            //如果传了应用标识，则优先取应用下的配置模板
        } elseif ($app) {
            $configFile = app()->getRootPath() . DS . 'app' . DS . $app . DS . 'view' . DS . 'config' . DS . 'config.html';
            $viewFile = is_file($configFile) ? $configFile : '';
        } else {
            $viewFile = '';
        }
        if ($this->request->isAjax()) {
            return $this->success('success', ['show' => $data, 'app' => $app]);
        }
        return view($viewFile, ['show' => $data, 'app' => $app]);
    }


    public function setMemberValues()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            $res = ConfigModel::setValues($param, 0, 'member', true);
            if (!$res) return $this->error(ConfigModel::getError('设置失败'));
            return $this->success('更新成功');
        }

    }

    public function setMemberValuesNew()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            // 20230621 判断是不是租户后台请求
            $member_id = 0;
            if ($this->scopes == "member") {
                $member_id = $this->uid;//获取中间件自动挂取的租户用户ID
            }
            //获取租户应用目录
            $app = $param['app'] ?? "";
            if (!$app) {
                $app_temp = app('http')->getName();
                $app = !in_array($app_temp, ['manage', 'install', 'api', 'home']) ? $app_temp : $app;
            }

            $res = ConfigModel::setValuesNew($param, $member_id, 'member', $app, true);
            if (!$res) return $this->error(ConfigModel::getError('设置失败'));
            return $this->success('更新成功');
        }

    }


}
